/*
 * (C) Copyright 2006-2010 Nuxeo SAS (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *     bstefanescu
 */
package org.nuxeo.gwt.habyt.app.client.form.bindings;

import java.io.Serializable;

import org.nuxeo.gwt.habyt.app.client.form.FormControl;
import org.nuxeo.gwt.habyt.templates.client.bindings.Helper;

import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.RadioButton;

/**
 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
 * 
 */
public class RadioGroupFactory extends AbstractControlFactory {

    public RadioGroupFactory() {
        super("radio");
    }

    @Override
    protected FormControl createControl(Element element) {
        String name = Helper.getAttribute(element, "name");
        RadioGroup w = new RadioGroup(name);
        Element child = Helper.getFirstChildElement(element, "g:item");
        while (child != null) {
            String disabled = Helper.getAttribute(child, "disabled");
            String checked = Helper.getAttribute(child, "checked");
            String value = Helper.getAttribute(child, "value");
            String label = child.getInnerHTML();
            w.addItem(value, label, Boolean.parseBoolean(checked),
                    Boolean.parseBoolean(disabled));
            child = Helper.getNextSiblingElement(child, "g:item");
        }
        return new FormControl(w) {
            @Override
            public String getName() {
                return ((RadioGroup) getWidget()).getName();
            }

            @Override
            public Serializable getValue() {
                return ((RadioGroup) getWidget()).getValue();
            }

            public void setValue(Serializable value) {
                if (value != null) {
                    ((RadioGroup) getWidget()).setValue(value.toString());
                }
            }
        };
    }

    public static class RadioGroup extends FlowPanel {
        protected String name;

        public RadioGroup(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public void addItem(String value, String label, boolean isChecked,
                boolean isDisabled) {
            if (value == null || value.length() == 0) {
                value = "" + getWidgetCount();
            }
            RadioButton rb = new RadioItem(name, label, value);
            rb.setValue(isChecked);
            rb.setEnabled(!isDisabled);
            rb.addStyleName("radio-item");
            add(rb);
        }

        public String getValue() {
            for (int i = 0, len = getWidgetCount(); i < len; i++) {
                RadioItem rb = (RadioItem) getWidget(i);
                if (rb.getValue()) {
                    return rb.getItemValue();
                }
            }
            return null;
        }

        public void setValue(String value) {
            for (int i = 0, len = getWidgetCount(); i < len; i++) {
                RadioItem rb = (RadioItem) getWidget(i);
                if (value.equals(rb.getItemValue())) {
                    rb.setValue(true);
                }
            }
        }

    }

    public static class RadioItem extends RadioButton {
        protected String value;

        public RadioItem(String name, String label, String value) {
            super(name, label, true);
            this.value = value;
        }

        public String getItemValue() {
            return value;
        }

        public void setItemValue(String value) {
            this.value = value;
        }

    }

}
